<?php
declare(strict_types=1);
namespace App\Controller\Cash\v1;

use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\CashMiddleware;
use Service\MemberService\MemberService;
use Service\AddressService\AddressService;
use Service\MemberService\BalanceService;

/**
 * 会员控制器
 * @author Luoshumai
 * @Controller(prefix="/cash/v1/member")
 * @Middleware(CashMiddleware::class)
 */
class MemberController extends AbstractController
{
    /**
     * @Inject
     * @var MemberService
     */
    protected $memberService;
    
    /**
     * @Inject
     * @var AddressService
     */
    protected $addressService;
    
    /**
     * @Inject
     * @var BalanceService
     */
    protected $balanceService;

    /**
     * 获取会员列表
     * @RequestMapping(path="memberlist", methods="post")
     */
    public function getMemberList(RequestInterface $request)
    {
        return $this->memberService->getMembers($request->getAttribute('cid'),$request->getAttribute('mn_sid'),$request->getParsedBody());
    }

    /**
     * 获取会员详情
     * @RequestMapping(path="memberinfo", methods="post")
     */
    public function getMemberInfo(RequestInterface $request)
    {
        return $this->memberService->getMemberInfo($request->getAttribute('cid'),$request->getAttribute('mn_sid'),$request->getParsedBody());
    }

    /**
     * 储值积分卡充值包获取
     * @RequestMapping(path="pointsbackage",methods="post")
     * @return array
     */
    public function pointsbackage(RequestInterface $request){
        $param = $request->all();
        $param['mn_sid'] = $request->getAttribute('mn_sid');
        $param['cid'] = $request->getAttribute('cid');
        return $this->balanceService->pointsbackage($param);
    }

    /**
     * 新增会员和收货地址
     * @RequestMapping(path="addma",methods="post")
     * @return array
     */
    public function addma(RequestInterface $request){
        $param = $request->all();
        $param['mn_sid'] = $request->getAttribute('mn_sid');
        $param['cid'] = $request->getAttribute('cid');
        $param['manager_name']=$request->getAttribute('manager_name');
        return $this->memberService->addma($param);
    }

    /**
     * 新增收货地址
     * @RequestMapping(path="addaddress",methods="post")
     * @return array
     */
    public function addaddress(RequestInterface $request){
        $param = $request->all();
        $param['mn_sid'] = $request->getAttribute('mn_sid');
        $param['cid'] = $request->getAttribute('cid');
        $param['is_default']=1;
        return $this->addressService->newAddress($param);
    }
    
    /**
     * 修改收货地址
     * @RequestMapping(path="editaddress",methods="post")
     * @return array
     */
    public function editaddress(RequestInterface $request){
        $param = $request->all();
        $param['mn_sid'] = $request->getAttribute('mn_sid');
        $param['cid'] = $request->getAttribute('cid');
        $param['is_default']=1;
        return $this->addressService->editAddress($param);
    }
    
    /**
     * 充值储值积分
     * @RequestMapping(path="chargepoints",methods="post")
     * @return array
     */
    public function chargepoints(RequestInterface $request){
        $param = $request->all();
        $param['mn_sid'] = $request->getAttribute('mn_sid');
        $param['cid'] = $request->getAttribute('cid');
        $param['manager_name'] = $request->getAttribute('manager_name');
        $param['cashregister_id'] = $request->getAttribute('casher_id');
        $param['casher'] = $request->getAttribute('manager_id');
        return $this->balanceService->chargepoints($param);
    }


    /**
     * 开通会员
     * @RequestMapping(path="addmember",methods="post")
     * @return array
     */
    public function registerMember(RequestInterface $request)
    {
        return  $this->memberService->registerMember(
            $request->getAttribute('mn_sid'),
            $request->getAttribute('cid'),
            $request->getAttribute('manager_name'),
            $request->getParsedBody()
        );
    }

    /**
     * 开通会员新版
     * @RequestMapping(path="createmember",methods="post")
     * @return array
     */
    public function registerMemberNew(RequestInterface $request)
    {
        return  $this->memberService->registerMemberNew(
            $request->getAttribute('cid'),
            $request->getAttribute('mn_sid'),
            $request->getAttribute('manager_name'),
            $request->getAttribute('manager_id'),
            $request->getAttribute('casher_id'),
            $request->getParsedBody()
        );
    }

    /**
     * 升级/续费会员
     * @RequestMapping(path="memberlevelup",methods="post")
     * @return array
     */
    public function memberLevelUp(RequestInterface $request)
    {
        return $this->memberService->memberLevelUp(
            $request->getAttribute('cid'),
            $request->getAttribute('mn_sid'),
            $request->getAttribute('manager_name'),
            $request->getAttribute('casher_id'),
            $request->getAttribute('manager_id'),
            $request->getParsedBody()
        );

    }

    /**
     * 确认购买人信息接口
     * @RequestMapping(path="memberAuth",methods="post")
     * @return array
     */
    public function memberAuth(RequestInterface $request)
    {
        return $this->memberService->memberAuth(
            $request->getAttribute('cid'),
            $request->getAttribute('mn_sid'),
            $request->getParsedBody()
        );
    }
    
    /**
     * 根据购买人身份证获取上次购买人电话和会员信息
     * @RequestMapping(path="getMemberInfoByIdcard",methods="post")
     * @return array
     */
    public function getMemberInfoByIdcard(RequestInterface $request)
    {
        return $this->memberService->getMemberInfoByIdcard(
            $request->getAttribute('cid'),
            $request->getAttribute('mn_sid'),
            $request->getParsedBody()
        );
    }
}